<template>
    <div class="mod-config">

        <el-table
                :data="dataList"
                border
                v-loading="dataListLoading"
                @selection-change="selectionChangeHandle"
                style="width: 100%;">
            <el-table-column
                    type="selection"
                    header-align="center"
                    align="center"
                    width="50">
            </el-table-column>
            <el-table-column
                    prop="taskName"
                    header-align="center"
                    align="center"
                    label="任务名称">
            </el-table-column>
            <el-table-column
                    prop="taskType"
                    header-align="center"
                    align="center"
                    label="任务类型">
            </el-table-column>
            <el-table-column
                    prop="exeUrl"
                    header-align="center"
                    align="center"
                    label="执行的url">
            </el-table-column>
            <el-table-column
                    prop="title"
                    header-align="center"
                    align="center"
                    label="目录名">
            </el-table-column>
            <el-table-column
                    prop="content"
                    header-align="center"
                    align="center"
                    label="内容">
                <template slot-scope="scope">
                    <el-button type="text" @click="open(scope.row.title,scope.row.content)">查看文章</el-button>
                </template>
            </el-table-column>
            <el-table-column
                    prop="state"
                    header-align="center"
                    align="center"
                    label="执行结果">
            </el-table-column>
            <el-table-column
                    prop="createTime"
                    header-align="center"
                    align="center"
                    label="执行时间">
            </el-table-column>
            <el-table-column
                    fixed="right"
                    header-align="center"
                    align="center"
                    width="150"
                    label="操作">
                <template slot-scope="scope">
                    <el-button v-if="scope.row.isPush==0" type="text" size="small" @click="pushArticle(scope.row.id,0)">推送当前文章</el-button>
                    <el-button v-if="scope.row.isPush==0" type="text" size="small" @click="pushArticle(scope.row.id,1)">推送任务文章</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
                @size-change="sizeChangeHandle"
                @current-change="currentChangeHandle"
                :current-page="pageIndex"
                :page-sizes="[10, 20, 50, 100]"
                :page-size="pageSize"
                :total="totalPage"
                layout="total, sizes, prev, pager, next, jumper">
        </el-pagination>


        <el-dialog
                :visible.sync="contentOpenVisible"
                width="80%"
                center>
            <span v-html="showContent"></span>
            <span slot="footer" class="dialog-footer">
                <el-button @click="contentOpenVisible = false">取 消</el-button>
            </span>
        </el-dialog>

    </div>
</template>

<script>
    import AddOrUpdate from './dataresult-add-or-update'

    export default {
        data() {
            return {
                dataForm: {
                    key: ''
                },
                dataList: [],
                pageIndex: 1,
                pageSize: 10,
                totalPage: 0,
                dataListLoading: false,
                dataListSelections: [],
                addOrUpdateVisible: false,

                contentOpenVisible:false,
                showContent:{}

            }
        },
        components: {
            AddOrUpdate
        },
        activated() {
            this.getDataList()
        },
        methods: {
            // 获取数据列表
            getDataList() {
                this.dataListLoading = true
                this.$api.DataResult.list({
                    'page': this.pageIndex,
                    'limit': this.pageSize,
                    'key': this.dataForm.key
                }).then((data) => {
                    if (data && data.code === 0) {
                        this.dataList = data.page.list;
                        this.totalPage = data.page.totalCount;

                        // 0失败，1、成功
                        this.dataList = data.page.list.map(item => {

                            let temp = item.state == 0 ? '失败' : '成功';
                            item.state = temp;

                            temp = item.taskType == 1 ? '小说' : '新闻';
                            item.taskType = temp;

                            return item;
                        });

                    } else {
                        this.dataList = []
                        this.totalPage = 0
                    }
                    this.dataListLoading = false
                })
            },
            // 每页数
            sizeChangeHandle(val) {
                this.pageSize = val
                this.pageIndex = 1
                this.getDataList()
            },
            // 当前页
            currentChangeHandle(val) {
                this.pageIndex = val
                this.getDataList()
            },
            // 多选
            selectionChangeHandle(val) {
                this.dataListSelections = val
            },
            // 新增 / 修改
            addOrUpdateHandle(id) {
                this.addOrUpdateVisible = true
                this.$nextTick(() => {
                    this.$refs.addOrUpdate.init(id)
                })
            },
            //内容展示
            open(title, content) {
                this.contentOpenVisible = true;
                this.showContent = content;
            },
            //推送到文章
            pushArticle(id,pushType) {
                let data = {
                    id:id,
                    pushType:pushType,
                }
                this.$api.DataResult.push(data).then((data) => {
                    if (data && data.code === 0) {
                        this.$message({message: '推送成功！', type: 'success'})
                    }
                }).catch(data =>{
                    this.$message.error(data.msg)
                })
            },
        }
    }
</script>
